package com.yhsoft.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.sys.controller.request.InterfaceConfigInVo;
import com.yhsoft.sys.entity.BaicInterfaceConfig;
import com.yhsoft.sys.mapper.BaicInterfaceConfigMapper;
import com.yhsoft.sys.service.IBaicInterfaceConfigService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 接口信息配置表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-10
 */
@Service
public class BaicInterfaceConfigServiceImpl extends ServiceImpl<BaicInterfaceConfigMapper, BaicInterfaceConfig> implements IBaicInterfaceConfigService {

    @Override
    public void setInterfaceConfig(InterfaceConfigInVo inVo) {
        if (inVo.getId() == null) {
            Integer companyid = TokenService.getUserBytoken().getCompanyId();
            BaicInterfaceConfig config = new BaicInterfaceConfig();
            config.setCompanyId(companyid);
            config.setAppName(inVo.getAppName());
            config.setAppId(inVo.getAppId());
            config.setAppQrCode(inVo.getAppQrCode());
            config.setNoticeUrl(inVo.getNoticeUrl());
            config.setMchid(inVo.getMchid());
            config.setPackageAppkey(inVo.getPackageAppkey());
            config.setPackageAppsecret(inVo.getPackageAppsecret());
            this.baseMapper.insert(config);
        } else {
            LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaicInterfaceConfig::getId, inVo.getId());
            BaicInterfaceConfig config = this.getBaseMapper().selectOne(wrapper);
            config.setAppName(inVo.getAppName());
            config.setAppId(inVo.getAppId());
            config.setAppQrCode(inVo.getAppQrCode());
            config.setNoticeUrl(inVo.getNoticeUrl());
            config.setMchid(inVo.getMchid());
            config.setPackageAppkey(inVo.getPackageAppkey());
            config.setPackageAppsecret(inVo.getPackageAppsecret());
            this.baseMapper.updateById(config);
        }
    }

    @Override
    public boolean exists(Integer companyId) {
        LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaicInterfaceConfig::getCompanyId, companyId);
        BaicInterfaceConfig baicInterfaceConfig = this.getBaseMapper().selectOne(wrapper);
        if(baicInterfaceConfig != null && StringUtils.hasLength(baicInterfaceConfig.getAppId())){
            return true;
        }
        return false;
    }
}
